package com.qinsoft.test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import org.junit.Test;

import com.google.gson.Gson;
import com.qinsoft.business.recom.bean.Recom;
import com.qinsoft.business.user.bean.User;
import com.qinsoft.frame.exception.ApiException;
import com.qinsoft.frame.utils.EmptyUtils;
import com.qinsoft.frame.utils.TaobaoInter;
import com.taobao.api.domain.Item;
import com.taobao.api.domain.Sku;
import com.taobao.api.response.ItemGetResponse;
import com.taobao.api.response.ItemSkuUpdateResponse;
import com.taobao.api.response.ItemsOnsaleGetResponse;

public class Demo
{

    @Test
    public void getString() throws ApiException
    {
        String tql = "select num_iid from taobao.items.onsale.get where page_no=3 and page_size=40";
        List<Item> list = TaobaoInter.sendInter(tql, ItemsOnsaleGetResponse.class).getItems();
        for (Item Iitem : list)
        {

            String tql1 = "select price,sku from item where num_iid=" + Iitem.getNumIid();
            Item item = TaobaoInter.sendInter(tql1, ItemGetResponse.class).getItem();
            for (Sku sku : item.getSkus())
            {
                double price = Double.parseDouble(sku.getPrice()) * 2 + 20.0;
                String skuTql = "update taobao.item.sku.price.update set item_price=" + price + ",price=" + price + ",properties=" + sku.getProperties() + " where num_iid=" + Iitem.getNumIid();
                TaobaoInter.sendInter(skuTql, ItemSkuUpdateResponse.class);
            }
            System.out.println(Iitem.getNumIid() + "完成");
        }
    }
    @Test
    public void test(){
    	String sql = "select num_iid,title,pic_url,price from taobao.items.onsale.get";
    	try {
			ItemsOnsaleGetResponse res = TaobaoInter.sendInter(sql, ItemsOnsaleGetResponse.class);
			if(!EmptyUtils.isEmptyList(res.getItems())){
				StringBuffer str = new StringBuffer();
				for(Item item : res.getItems()){
					str.append("商品名称："+item.getTitle()).append("  商品价格："+item.getPrice()+"  商品链接：http://item.taobao.com/item.htm?id="+item.getNumIid()).append("  商品图片："+item.getPicUrl()+"__310x310.jpg").append("\r\n\r\n");
				}
				try{
				     BufferedWriter writer = new BufferedWriter(new FileWriter(new File("c:\\Result.txt"),true));
				     writer.write(str+"");
				     writer.close();
				}catch(Exception e){
				}
			}
		} catch (ApiException e) {
			e.printStackTrace();
		}
    }
    @Test
    public void json(){
    	LinkedList<Object> list = new LinkedList<Object>();
    	User u = new User();
    	u.setNick("zj");
    	list.add(u);
    	for (Iterator iter = list.iterator(); iter.hasNext();) {
    		User u1 = (User)iter.next();
    		System.out.println(u1.getNick());
    	}
    	list.remove("zj");
    	for (Iterator iter = list.iterator(); iter.hasNext();) {
    		User u2 = (User)iter.next();
    		System.out.println(u2.getNick());
    	}
    } 
}
class A{
	Integer ages;
	int age;
	public Integer getAges() {
		return ages;
	}
	public void setAges(Integer ages) {
		this.ages = ages;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}
